<?php

namespace app\api\controller\notice;

use app\common\controller\Api;
use think\Config;

/**
 * 公告接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        $this->acticle=model('admin/notice/Acticle');
        $this->category=model('admin/notice/Category');
        $this->hotcategory=model('admin/notice/Hotcategory');
        $this->hotmessage=model('admin/notice/Hotmessage');


    }

    /**
     * 分类
     */
  public function  category(){
     $category= $this->category->order('weigh asc')->select();
     foreach ($category as $key=>$value){
         $category[$key]['articleNum']=$this->acticle->where('notice_category_id',$value['id'])->count();
     }
        $this->success('OK',$category);
  }

  /**
   *公告列表
   */
  public function articleList($page=1,$limit=10,$notice_category_id=null){
     $where=[];
      if($notice_category_id!=null){
          $where['notice_category_id']=['=',$notice_category_id];
      }
     $article= $this->acticle->where($where)->order('createtime desc')->paginate($limit,false);
      foreach ($article as $key=>$value){
          $article[$key]['year_month']=date("Y-m",$value['createtime']);
          $article[$key]['day']=date("d",$value['createtime']);
      }

      $this->success('OK',$article);

  }

    /**
     * 公告详情
     */
  public function articleDetails($id){
      $where['id']=['=',$id];
      $article= $this->acticle->get($where);
      if($article){
          $article['notice_category_name']=$this->category->where('id',$article->notice_category_id)->value('name');
          $article['getPrevArticle']=$this->getPrevArticle($id);
          $article['getNextArticle']=$this->getNextArticle($id);
          $article['createtime']=date('Y-m-d H:i:s',$article['createtime']);
          $this->success('OK',$article);
      }else{
          $this->error('未找到数据');
      }
  }

    /**
     * 上一篇
     * @param $id
     * @return null
     */
    public function getPrevArticle($id)
    {
        $where['id']=['=',$id];

        $article= $this->acticle->get($where);
        if($article){
            $prevArticle = $this->acticle->where('notice_category_id', '=', $article->notice_category_id)
                ->where('createtime', '<', $article->createtime)
                ->order('createtime', 'desc')
                ->field('id,title')
                ->limit(1)
                ->find();
        }else{
            $prevArticle=null;
        }

        return $prevArticle;
    }

    // 获取下一篇文章
    public function getNextArticle($id)
    { $where['id']=['=',$id];

        $article= $this->acticle->get($where);

        if($article){
            $nextArticle = $this->acticle->where('notice_category_id', '=', $article->notice_category_id)
                                 ->where('createtime', '>', $article->createtime)
                ->order('createtime', 'desc')
                ->field('id,title')
                ->limit(1)
                ->find();

        } else{
            $nextArticle= null;
        }
        return $nextArticle;

    }
/**
 * 我们的优势
 */

 public  function hotcategory(){
    $hotcategory= $this->hotcategory->order('weigh asc')->select();

    foreach ($hotcategory as $key=>$value){
        $hotcategory[$key]['hotmessage']=$this->hotmessage->order('weigh asc')->where('notice_hotcategory_id',$value['id'])->select();

    }
     $this->success('OK',$hotcategory);
 }



}
